package com.holidaygo.activities;

import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.holidaygo.db.JSONParser;
import com.holidaygo.db.MySingleton;
import com.holidaygo.activities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class VacRen_search extends Activity {

	// url to make request
	private static String url1 = "http://www.holidaygo.com/web_service?ajax=1&action=select_countries";
	private static String url2 = "http://www.holidaygo.com/web_service?ajax=1&action=select_regions&country=";
	private static String url3 = "http://www.holidaygo.com/web_service?ajax=1&action=select_cities&region=";



	private static Boolean is_country_picked = false;
	private static Boolean is_region_picked = false;
	private static Boolean is_city_picked = false;
	private static String selected_country;
	private static String selected_region;
	private static String selected_city;
	private String selectedCode;

	// JSON Node names
	private static final String TAG_COUNTRY_CODE = "Country_str_code";
	private static final String TAG_COUNTRY_NAME = "Country_str_name";
	private static final String TAG_COUNT = "Count";

	private static final String TAG_REGION_CODE = "Admin1_str_code";
	private static final String TAG_REGION_NAME = "Admin1_str_name";
	private static final String TAG_SUBREGION = "Subregion";

	private static final String TAG_CITY_ID = "Feature_int_id";
	private static final String TAG_CITY_NAME = "Feature_str_name";


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vacation_rentals_search);

		// Creating JSON Parser instance
		// getting JSON string from URL
		JSONParser jParser1 = new JSONParser();
		JSONArray json1 = jParser1.getJSONfromURL(url1);

		//COUNTRIES
		MySingleton.getInstance().countries.clear();
		try {
			// Getting Array of Countries
			// looping through All Countries
			for(int i = 0; i < json1.length(); i++){
				JSONObject c = json1.getJSONObject(i);

				// Storing each json item in variable
				String code = c.getString(TAG_COUNTRY_CODE);
				String name = c.getString(TAG_COUNTRY_NAME);
				String count = c.getString(TAG_COUNT);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_COUNTRY_CODE, code);
				map.put(TAG_COUNTRY_NAME, name);
				map.put(TAG_COUNT, count);

				// adding HashList to ArrayList
				MySingleton.getInstance().countries.add(map);
			}

			int i=1;
			String[] countries_list=new String[json1.length()+1];
			countries_list[0] = "Select Country";
			for (HashMap<String, String> hmap: MySingleton.getInstance().countries)
			{
				countries_list[i]=hmap.get(TAG_COUNTRY_NAME);
				i++;
			}

			TextView name = (TextView)findViewById(R.id.name);
			Spinner spinner1 = (Spinner) findViewById(R.id.s_country);

			spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
			{
				public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
				{
					selected_country = (String) ((TextView) arg1).getText();
					selectedCode=null;
					if (arg2!=0)
					{
						is_country_picked = true;
						selectedCode=MySingleton.getInstance().countries.get(arg2-1).get(TAG_COUNTRY_CODE);
						//Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),Toast.LENGTH_SHORT).show();
						//Toast.makeText(getApplicationContext(), "please select a countryskdfj first",Toast.LENGTH_SHORT).show();
					}

					////////////region
					MySingleton.getInstance().regions.clear();
					JSONParser jParser2 = new JSONParser();
					JSONArray json2 = jParser2.getJSONfromURL(url2+selectedCode);
					for(int i = 0; i < json2.length(); i++)
					{
						JSONObject c;
						try {
							c = json2.getJSONObject(i);
							// Storing each json item in variable
							String code = c.getString(TAG_REGION_CODE);
							String name1 = c.getString(TAG_REGION_NAME);
							String count = c.getString(TAG_COUNT);
							String subregion=c.getString(TAG_SUBREGION);
							// creating new HashMap
							HashMap<String, String> map = new HashMap<String, String>();
							// adding each child node to HashMap key => value
							map.put(TAG_REGION_CODE, code);
							map.put(TAG_REGION_NAME, name1);
							map.put(TAG_COUNT, count);
							map.put(TAG_SUBREGION, subregion);
							// adding HashList to ArrayList
							MySingleton.getInstance().regions.add(map);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					int i=1;
					String[] regions_list=new String[json2.length()+1];
					regions_list[0] = "Select Region";
					for (HashMap<String, String> hmap: MySingleton.getInstance().regions)
					{
						regions_list[i]=hmap.get(TAG_REGION_NAME);
						i++;
					}
					Spinner spinner2 = (Spinner) findViewById(R.id.s_region);		
					ArrayAdapter spinner2ArrayAdapter = new ArrayAdapter(VacRen_search.this, android.R.layout.simple_spinner_item, regions_list);
					spinner2ArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					spinner2.setAdapter(spinner2ArrayAdapter);				
					/////////////end region							

				} //end Countries-OnItemSelected
				public void onNothingSelected(AdapterView<?> arg0) {}
			}); 
			ArrayAdapter spinner1ArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, countries_list);
			spinner1ArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner1.setAdapter(spinner1ArrayAdapter);


			//REGIONS
			MySingleton.getInstance().regions.clear();
			JSONParser jParser2 = new JSONParser();
			JSONArray json2 = jParser2.getJSONfromURL(url2);

			for(i = 0; i < json2.length(); i++){
				JSONObject c = json2.getJSONObject(i);
				// Storing each json item in variable
				String code = c.getString(TAG_REGION_CODE);
				String name1 = c.getString(TAG_REGION_NAME);
				String count = c.getString(TAG_COUNT);
				String subregion=c.getString(TAG_SUBREGION);
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				// adding each child node to HashMap key => value
				map.put(TAG_REGION_CODE, code);
				map.put(TAG_REGION_NAME, name1);
				map.put(TAG_COUNT, count);
				map.put(TAG_SUBREGION, subregion);
				// adding HashList to ArrayList
				MySingleton.getInstance().regions.add(map);
			}

			i=1;
			String[] regions_list=new String[json2.length()+1];
			regions_list[0] = "Select Region";
			for (HashMap<String, String> hmap: MySingleton.getInstance().regions)
			{
				regions_list[i]=hmap.get(TAG_REGION_NAME);
				i++;
			}
			Spinner spinner2 = (Spinner) findViewById(R.id.s_region);		
			spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
				public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
					if (arg2!=0)
					{
						if (is_country_picked==false)
						{
							Toast.makeText(getApplicationContext(), "please select a country first",Toast.LENGTH_SHORT).show();
							//Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),Toast.LENGTH_SHORT).show();
						}
						else 
						{
							selected_region = (String) ((TextView) arg1).getText();
							selectedCode=null;
							if (arg2!=0)
							{
								is_region_picked = true;
								selectedCode=MySingleton.getInstance().regions.get(arg2-1).get(TAG_REGION_CODE);
							}
							//////////city
							MySingleton.getInstance().city.clear();
							JSONParser jParser3 = new JSONParser();
							JSONArray json3 = jParser3.getJSONfromURL(url3+selectedCode);

							for(int i = 0; i < json3.length(); i++){
								JSONObject c;
								try {
									c = json3.getJSONObject(i);
									// Storing each json item in variable
									String code = c.getString(TAG_CITY_ID);
									String name3 = c.getString(TAG_CITY_NAME);
									String count = c.getString(TAG_COUNT);
									// creating new HashMap
									HashMap<String, String> map = new HashMap<String, String>();
									// adding each child node to HashMap key => value
									map.put(TAG_CITY_ID, code);
									map.put(TAG_CITY_NAME, name3);
									map.put(TAG_COUNT, count);
									// adding HashList to ArrayList
									MySingleton.getInstance().city.add(map);
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							int i=1;
							String[] cities_list=new String[json3.length()+1];
							cities_list[0] = "Select City";
							for (HashMap<String, String> hmap: MySingleton.getInstance().city)
							{
								cities_list[i]=hmap.get(TAG_CITY_NAME);
								i++;
							}
							Spinner spinner3 = (Spinner) findViewById(R.id.s_city);
							ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(VacRen_search.this, android.R.layout.simple_spinner_item, cities_list);
							spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
							spinner3.setAdapter(spinnerArrayAdapter);

							/////////end city				
							//Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),Toast.LENGTH_SHORT).show();
							//if (selected_region!="Select Region"){
							is_region_picked = true;		
						} 
					}
					/*else
					{
						Toast.makeText(getApplicationContext(), "please select a country22 first",Toast.LENGTH_SHORT).show();
					}*/
				}

				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});

			ArrayAdapter spinner2ArrayAdapter = new ArrayAdapter(
					this, android.R.layout.simple_spinner_item, regions_list);
			spinner2ArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner2.setAdapter(spinner2ArrayAdapter);



			//CITY
			MySingleton.getInstance().city.clear();
			JSONParser jParser3 = new JSONParser();
			JSONArray json3 = jParser3.getJSONfromURL(url3);

			for(i = 0; i < json3.length(); i++){
				JSONObject c = json3.getJSONObject(i);
				// Storing each json item in variable
				String code = c.getString(TAG_CITY_ID);
				String name3 = c.getString(TAG_CITY_NAME);
				String count = c.getString(TAG_COUNT);
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				// adding each child node to HashMap key => value
				map.put(TAG_CITY_ID, code);
				map.put(TAG_CITY_NAME, name3);
				map.put(TAG_COUNT, count);
				// adding HashList to ArrayList
				MySingleton.getInstance().city.add(map);
			}

			i=1;
			String[] cities_list=new String[json3.length()+1];
			cities_list[0] = "Select City";
			for (HashMap<String, String> hmap: MySingleton.getInstance().city)
			{
				cities_list[i]=hmap.get(TAG_CITY_NAME);
				i++;
			}
			Spinner spinner3 = (Spinner) findViewById(R.id.s_city);

			spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
				{
					if (arg2!=0){
						if (is_region_picked==false)
						{
							Toast.makeText(getApplicationContext(), "please select a region first",Toast.LENGTH_SHORT).show();
						}
						else{
							selected_city = (String) ((TextView) arg1).getText();
							selectedCode=null;
							if (arg2!=0)
							{
								is_city_picked = true;
								selectedCode=MySingleton.getInstance().city.get(arg2-1).get(TAG_CITY_ID);
							}
						}
					}
				}
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});

			ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, cities_list);
			spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner3.setAdapter(spinnerArrayAdapter);

		} catch (JSONException e) {
			e.printStackTrace();
		}

		/////////////// Search button
		Button btn = (Button) findViewById(R.id.b_search);
		btn.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View arg0)
			{
				Intent intent = new Intent(VacRen_search.this, VacRen_results.class);
				intent.putExtra("code", selectedCode);
				intent.putExtra("country", selected_country);
				intent.putExtra("region", selected_region);
				if (selected_city!=null){
					intent.putExtra("area", "settlement");
				}else{
					intent.putExtra("area", "region");
				}
				// Set the request code to any code you like, you can identify the
				// callback via this code
				//startActivityForResult(intent, REQUEST_CODE);
				
				startActivity(intent);
			}
		});
	}
}
